<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/16
 * Time: 12:01
 */

namespace app\admin\controller;

use app\admin\model\AdminMenu;
use app\admin\model\AdminRole;
use app\admin\model\AdminUser;
use think\facade\Session;

class Index extends Base
{

    public function index(){
        $admin_id = Session::get('admin_id');
        $admin = AdminUser::where('id',$admin_id)->field('id,username,role_id')->find();
        $role  = AdminRole::where('id',$admin['role_id'])->where('status',1)->find();
        $role['rule'] = explode(',',$role['rule']);
        $rule = AdminMenu::where('id','in',$role['rule'])
            ->where('type',1)->order('sort desc')->where('status',1)->select()->toArray();
        $ret = $this->roleArray($rule);
        foreach ($ret as $key=>$value){
            if ($value['subset'] && $value['action']){
                $ret[$key]['url'] = $value['subset'] ."/". $value['action'];
            }else{
                $ret[$key]['url'] = '';
            }
            if (is_array($value['subset'])){
                foreach ($value['subset'] as $k=>$v){
                    $ret[$key]['subset'][$k]['url'] = $v['controller']."/".$v['action'];
                }
            }else{
                $ret[$key]['subset'] = [];
            }
        }
        $this->assign('admin',$admin);
        $this->assign('menu',$ret);
        return $this->fetch();
    }

    public function welcome(){
        $admin_id = Session::get('admin_id');
        $admin = AdminUser::where('id',$admin_id)->field('id,username')->find();
        $this->assign('admin',$admin);
        return $this->fetch();
    }





}